package com.wei.czz.framework.blog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wei.czz.common.enums.CommonEnum;
import com.wei.czz.common.po.UserPo;
import com.wei.czz.common.po.blog.audit.groupUser.BlogAuditGroupUserPo;
import com.wei.czz.common.tool.PageDto;
import com.wei.czz.common.utils.SecurityUtils;
import com.wei.czz.common.vo.blog.audit.groupUser.BlogAuditGroupUserFormVo;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.Date;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.wei.czz.framework.blog.dao.BlogAuditGroupUserDao;
import com.wei.czz.framework.blog.entity.BlogAuditGroupUserEntity;
import com.wei.czz.framework.blog.service.BlogAuditGroupUserService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

/**
 * Created by IntelliJ IDEA.
 *
 * @author wei
 * date: 2024-12-05 08:22:00
 * className: BlogAuditGroupUserServiceImpl
 * version: 1.0
 * description:
 */
@Service("blogAuditGroupUserService")
public class BlogAuditGroupUserServiceImpl extends ServiceImpl<BlogAuditGroupUserDao, BlogAuditGroupUserEntity>
        implements BlogAuditGroupUserService {

    private static final Logger log = LoggerFactory.getLogger(BlogAuditGroupUserServiceImpl.class);

    @Transactional
    @Override
    public void batchSave(List<BlogAuditGroupUserEntity> blogAuditGroupUserList) {
        boolean bool = this.saveBatch(blogAuditGroupUserList);
        log.info("批量保存博客审核分组用户完成。bool={}", bool);
    }

    @Override
    public PageDto<BlogAuditGroupUserPo> getPageList(BlogAuditGroupUserFormVo formVo) {
        // mp分页对象
        Page<BlogAuditGroupUserPo> page = new Page<>(formVo.getPage(), formVo.getLimit());

        // 分页查询
        page = baseMapper.selectPageList(page, formVo);
        log.info("分页查询博客审核分组用户完成。totalNum={} list.size={}", page.getTotal(), page.getRecords());
        return new PageDto<>(page);
    }

    @Override
    public List<BlogAuditGroupUserEntity> findList(List<Long> groupIdList) {
        if (CollectionUtils.isEmpty(groupIdList)) {
            log.info("获取博客审核分组用户，传递的分组主键列表为空");
            return Collections.emptyList();
        }

        LambdaQueryWrapper<BlogAuditGroupUserEntity> blogAuditGroupUserLambdaQueryWrapper = new LambdaQueryWrapper<>();
        blogAuditGroupUserLambdaQueryWrapper.in(BlogAuditGroupUserEntity::getGroupId, groupIdList)
                .eq(BlogAuditGroupUserEntity::getDeleteStatus, CommonEnum.ZERO.getValue());
        // 查询
        List<BlogAuditGroupUserEntity> blogAuditGroupUserList = baseMapper.selectList(blogAuditGroupUserLambdaQueryWrapper);
        log.info("查询博客审核分组用户完成。list.size={}", blogAuditGroupUserList.size());
        return blogAuditGroupUserList;
    }

    @Override
    public List<BlogAuditGroupUserPo> findEffectList(Long groupId) {
        // 查询
        List<BlogAuditGroupUserPo> blogAuditGroupUserPoList = baseMapper.selectEffectList(groupId,
                CommonEnum.ZERO.getValue(), CommonEnum.ZERO.getValue(), CommonEnum.ZERO.getValue());
        log.info("查询可用的博客审核分组用户完成。list.size={}", blogAuditGroupUserPoList.size());
        return blogAuditGroupUserPoList;
    }

    @Override
    public List<BlogAuditGroupUserEntity> getList() {

        LambdaQueryWrapper<BlogAuditGroupUserEntity> blogAuditGroupUserLambdaQueryWrapper = new LambdaQueryWrapper<>();
        blogAuditGroupUserLambdaQueryWrapper.eq(BlogAuditGroupUserEntity::getDeleteStatus, CommonEnum.ZERO.getValue());
        // 查询
        List<BlogAuditGroupUserEntity> blogAuditGroupUserList = baseMapper.selectList(blogAuditGroupUserLambdaQueryWrapper);
        log.info("查询所有博客审核分组用户完成。list.size={}", blogAuditGroupUserList.size());

        return blogAuditGroupUserList;
    }

    @Override
    public void delete(List<Long> idList, List<Long> groupIdList) {
        if (CollectionUtils.isEmpty(idList) && CollectionUtils.isEmpty(groupIdList)) {
            log.info("删除博客审核分组用户，传递的主键列表和分组主键列表均为空");
            return;
        }
        // 获取请求用户
        UserPo userPo = SecurityUtils.getUser();
        Date date = new Date();

        LambdaUpdateWrapper<BlogAuditGroupUserEntity> blogAuditGroupUserLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        blogAuditGroupUserLambdaUpdateWrapper.set(BlogAuditGroupUserEntity::getDeleteStatus, CommonEnum.ONE.getValue())
                .set(BlogAuditGroupUserEntity::getUpdateTime, date)
                .set(BlogAuditGroupUserEntity::getUpdateUser, userPo.getUsername())
                .set(BlogAuditGroupUserEntity::getUpdateUserId, userPo.getUserId());
        blogAuditGroupUserLambdaUpdateWrapper.in(!CollectionUtils.isEmpty(idList), BlogAuditGroupUserEntity::getUserId,
                idList)
                .in(!CollectionUtils.isEmpty(groupIdList), BlogAuditGroupUserEntity::getGroupId, groupIdList)
                .eq(BlogAuditGroupUserEntity::getDeleteStatus, CommonEnum.ZERO.getValue());
        // 删除
        int count = baseMapper.update(blogAuditGroupUserLambdaUpdateWrapper);
        log.info("删除博客审核分组用户完成。count={}", count);
    }

    @Transactional
    @Override
    public void batchUpdate(List<BlogAuditGroupUserEntity> blogAuditGroupUserList) {
        boolean bool = this.updateBatchById(blogAuditGroupUserList);
        log.info("批量更新博客审核分组用户完成。bool={}", bool);
    }
}